package com.zha.boss.client;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zha
 * @CreateTime: 2024年09月05日 14:40
 * redis 客户端类
 */
@Component
public class RedisOperator {
    private StringRedisTemplate stringRedisTemplate;
    private ValueOperations<String, String> valueOperations;
    private HashOperations<String, String, String> hashOperations;
    public RedisOperator(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        valueOperations = stringRedisTemplate.opsForValue();
        hashOperations = stringRedisTemplate.opsForHash();
    }
    // 创建hash类型
    public Boolean setHash(String key, Map<String, String> data , int timeoutMinute){
        hashOperations.putAll(key,data);
        return stringRedisTemplate.expire(key,timeoutMinute, TimeUnit.MINUTES);
    }
    // 删除key
    public Boolean delete(String key){
        return stringRedisTemplate.delete(key);
    }
    // 获取hash类型
    public String getField(String key, String field){
        return hashOperations.get(key,field);
    }
    // 创建String类型的方法
    public void setValue(String key, String value){
        valueOperations.set(key,value);
    }
    public String getValue(String key){
        return valueOperations.get(key);
    }
}
